package com.ihg.hiex.admin.web.controller.session.preparation;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.entity.session.RelationShip;
import com.ihg.hiex.admin.service.session.KnowingRelationService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.admin.web.view.ExcelTemplateView;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.session.meeting.Target;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;

/**
 * 
 * @author Guangli Yan
 *
 */
@Controller
@RequestMapping("/sessions/{sessionId}")
@MenuSetting("menu-sessions")
public class OrientationController {
    
    private static final Logger logger = LoggerFactory.getLogger(OrientationController.class);

    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private KnowingRelationService knowingRelationService;
    
    /**
     * orientation call index
     * 
     * @param login
     * @param sessionId
     * @return
     */
    @MenuSetting(value = "submenu-preparation-orientation")
    @RequestMapping(path = "/orientations", method = RequestMethod.GET)
    public ModelAndView orientation(@Secure Login login,@PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/orientation/orientation-list");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        // 1. get assessor & delegate orientation call meeting
        SessionMeeting assessorMeeting = sessionService.loadSessionMeeting(session, "Orientation Call", Target.ADMIN_ASSESSORS);
        modelAndView.addObject("assessorMeeting", assessorMeeting);
        
        SessionMeeting delegateMeeting = sessionService.loadSessionMeeting(session, "Orientation Call", Target.ADMIN_DELEGATES);
        modelAndView.addObject("delegateMeeting", delegateMeeting);
        
        // 2. list assessor & delegate
        List<Assessor> assessors = sessionService.listAssessor(session);
        modelAndView.addObject("assessors", assessors);
        
        List<Delegate> delegates = sessionService.listDelegate(session);
        modelAndView.addObject("delegates", delegates);
        
        return modelAndView;
        
    }
    
    /**
     * who-i-know index
     * 
     * @param login
     * @param sessionId
     * @return
     */
    @MenuSetting(value = "submenu-preparation-orientation")
    @RequestMapping(path = "/who-i-know", method = RequestMethod.GET)
    public ModelAndView whoIKnow(@Secure Login login, @PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/orientation/who-i-know-list");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        // 1. list assessor
        List<Assessor> assessors = sessionService.listAssessor(session);
        modelAndView.addObject("assessors", assessors);
        
        // 2. list knowing realtion
        List<Delegate> delegates = sessionService.listDelegate(session);
        List<RelationShip> relationShips = knowingRelationService.listRelationShip(session, assessors, delegates);
        modelAndView.addObject("relationShips", relationShips);
        
        return modelAndView;
        
    }
    
    /**
     * edit orientation call meeting
     * 
     * @param sessionId
     * @param meetingId
     * @return
     */
    @RequestMapping(path = "/orientations/{meetingId}/edit-date", method = RequestMethod.GET)
    public ModelAndView editDate(@PathVariable("sessionId") Long sessionId, 
                                 @PathVariable("meetingId") Long meetingId) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/orientation/orientation-edit-date");
        
        SessionMeeting sessionMeeting = sessionService.loadSessionMeeting(meetingId);
        modelAndView.addObject("sessionMeeting", sessionMeeting);
        
        modelAndView.addObject("sessionId", sessionId);
        
        return modelAndView;
        
    }
    
    /**
     * on edit orientation call meeting
     * 
     * @param sessionId
     * @param sessionMeeting
     * @return
     */
    @RequestMapping(value = "/orientations/{id}/edit-date" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditDate(@PathVariable("sessionId") Long sessionId,
                                               @Preloading("meetingId") SessionMeeting sessionMeeting) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            sessionService.editSessionMeeting(sessionMeeting);
            
            httpResult.success().message("修改时间成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("修改时间失败");
            logger.error("#onEditDate() - ",e);
        }
        
        return httpResult;
        
    }
    
    /**
     * 
     * @param sessionId
     * @return
     */
    @RequestMapping(value = "/who-i-know/download", method = RequestMethod.GET)
    public ModelAndView onExport(@PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("who-i-know.xls", "who-i-know.xls"));
        
        Session session = sessionService.load(sessionId);
        
        // 1. list assessor
        List<Assessor> assessors = sessionService.listAssessor(session);
        modelAndView.addObject("assessors", assessors);
        
        // 2. list knowing realtion
        List<Delegate> delegates = sessionService.listDelegate(session);
        List<RelationShip> relationShips = knowingRelationService.listRelationShip(session, assessors, delegates);
        modelAndView.addObject("relationShips", relationShips);
        
        return modelAndView;
        
    }
    
}
